package com.aviation.sell_ticket.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.aviation.sell_ticket.mapper.PassengerMapper;
import com.aviation.sell_ticket.pojo.Aviation_city;
import com.aviation.sell_ticket.pojo.Aviation_passenger;
import com.aviation.sell_ticket.service.PassengerService;
import com.aviation.sell_ticket.tool.ResultEnum;
import com.aviation.sell_ticket.tool.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class PassengerServiceImpl implements PassengerService {


    @Resource
    private PassengerMapper passengerMapper;

    /**
     * 根据用户id提取乘客信息
     * @param userId 用户Id
     * @return Map<String,Object>
     */
    @Override
    public Map<String,Object> passengerList(String userId) {
        try {
            List<Aviation_passenger> passengers = passengerMapper.passengerList(userId);
            if(passengers!=null && passengers.size()>0){
                return ResultUtil.success(ResultEnum.SUCCESS, passengers);
            }
        }catch (Exception e){
            return ResultUtil.beDefeated(ResultEnum.UNKNOWN_ERROR);
        }
        return ResultUtil.success(ResultEnum.SUCCESS, null);
    }

    /**
     * 新增乘客信息
     * @param passenger 乘客信息
     * @param userId 用户id
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> addPassenger(Aviation_passenger passenger, String userId) {
        try {
            //生成乘客id
            String passenger_id= IdUtil.simpleUUID();
            passenger.setPassenger_id(passenger_id);
            passenger.setUser_id(userId);

            //查询此用户的乘客信息是否重复或是否超出
            List<Aviation_passenger> passengers = passengerMapper.passengerList(userId);
            if(passengers.size()>=10){
                return ResultUtil.success(ResultEnum.PASSENGER_INFORMATION_UPPER_LIMIT,null);
            }else{
                for (Aviation_passenger aviationPassenger : passengers) {
                    if(aviationPassenger.getPassenger_name().equals(passenger.getPassenger_name())){
                        return ResultUtil.success(ResultEnum.ADDING_THE_SAME_PASSENGER_IS_NOT_ALLOWED,null);
                    }
                }
            }

            //添加乘客信息
            Integer addPassenger = passengerMapper.addPassenger(passenger);
            if(addPassenger>0){
                return ResultUtil.success(ResultEnum.SUCCESS, null);
            }else{
                return ResultUtil.success(ResultEnum.FAILED_TO_ADD_PASSENGER_INFORMATION, null);
            }

        }catch (Exception e){
            return ResultUtil.beDefeated(ResultEnum.UNKNOWN_ERROR);
        }
    }

    /**
     * 删除乘客信息
     * @param passenger_id 乘客信息id
     * @param userId 用户id
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> delPassenger(String passenger_id, String userId) {
        try {
            //删除乘客信息
            Integer delPassenger = passengerMapper.delPassenger(passenger_id, userId);
            if(delPassenger>0){
                return ResultUtil.success(ResultEnum.SUCCESS, null);
            }else{
                return ResultUtil.success(ResultEnum.FAILED_TO_DELETE_PASSENGER_INFORMATION, null);
            }
        }catch (Exception e){
            return ResultUtil.beDefeated(ResultEnum.UNKNOWN_ERROR);
        }
    }
}
